package com.itmiaojie.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itmiaojie.constant.MessageConstant;
import com.itmiaojie.constant.RedisMessageConstant;
import com.itmiaojie.entity.Result;
import com.itmiaojie.pojo.Order;
import com.itmiaojie.service.OrderService;
import com.itmiaojie.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;
    private String from = "1741672901@qq.com";	 // 发送人
    private String to = "1741672901@qq.com";	 // 接收人
    private String subject = "预约结果";		 // 邮件主题
    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping("/submit")
    public Result submitOrder(@RequestBody Map map){

        String code = (String) map.get("telephone");
        String cacheCode = jedisPool.getResource().get(code + RedisMessageConstant.SENDTYPE_ORDER);
        String validateCode = (String)map.get("validateCode");
        if (cacheCode!= null && !cacheCode.equals(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result =null;
        //调用体检预约服务
        try{
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            result = orderService.order(map);
        }catch (Exception e){
            e.printStackTrace();
            //预约失败
            return result;
        }
        if(result.isFlag()){
            //预约成功，发送短信通知
            String orderDate = (String) map.get("orderDate");
            try {
                //发送email
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                MimeMessageHelper mimeMessageHelper = MailUtils.send(mimeMessage,from,to,"预约成功！预约时间为："+orderDate + code.toString(),subject);
                javaMailSender.send(mimeMessage);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        return result;



    }


    /**
     * 根据id查询预约信息，包括套餐信息和会员信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Map map = orderService.findById(id);
            //查询预约信息成功
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            //查询预约信息失败
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

}
